<?php
abstract class Pageley_Domain_Validator_Abstract implements Pageley_Domain_Validator {
	protected function rejectIfEmpty($value, $propertyName, $message = "error.validation.empty") {
		$error = $this->rejectIfTrue(($value === null || $value === ""), $message, array("{field." . $propertyName . "}"));
		return $error;
	}

	protected function rejectIfGreater($value, $maxValue, $propertyName, $message = "error.validation.tooBig") {
		$error = null;
		if($value != null) {
			$error = $this->rejectIfTrue(($value > $maxValue), $message, array("{field." . $propertyName . "}", $value, $maxValue));
		}
		return $error;
	}

	protected function rejectIfSmaller($value, $minValue, $propertyName, $message = "error.validation.tooSmall") {
		$error = null;
		if($value != null) {
			$error = $this->rejectIfTrue(($value < $minValue), $message, array("{field." . $propertyName . "}", $value, $minValue));
		}
		return $error;
	}

	protected function rejectIfLonger($value, $maxLength, $propertyName, $message = "error.validation.tooLong") {
		$error = null;
		if($value != null) {
			$length = strlen($value);
			$error = $this->rejectIfTrue(($length > $maxLength), $message, array("{field." . $propertyName . "}", $value, $length, $maxLength));
		}
		return $error;
	}

	protected function rejectIfShorter($value, $minLength, $propertyName, $message = "error.validation.tooShort") {
		$error = null;
		if($value != null) {
			$length = strlen($value);
			$error = $this->rejectIfTrue(($length < $minLength), $message, array("{field." . $propertyName . "}", $value, $length, $minLength));
		}
		return $error;
	}

	protected function rejectIfEqual($value1, $value2, $propertyName, $message = "error.validation.equal") {
		$error = $this->rejectIfTrue(($value1 == $value2), $message, array("{field." . $propertyName . "}", $value1, $value2));
		return $error;
	}

	protected function rejectIfNotEqual($value1, $value2, $propertyName, $message = "error.validation.notEqual") {
		$error = $this->rejectIfTrue(($value1 != $value2), $message, array("{field." . $propertyName . "}", $value1, $value2));
		return $error;
	}

	protected function rejectIfNotMatches($value, $pattern, $propertyName, $message = "error.validation.invalid") {
		$pattern = Pageley_Util_String::replace($pattern, "/", "\/");
	    $error = $this->rejectIfTrue((!preg_match("/" . $pattern . "/", $value)), $message, array("{field." . $propertyName . "}", $value));
		return $error;
	}

	protected function rejectIfTrue($condition, $messageKey, $messageArguments) {
		return $condition ? new Pageley_I18n_Message($messageKey, $messageArguments) : null;
	}

	protected function rejectIfFalse($condition, $messageKey, $messageArguments) {
		$error = $this->rejectIfTrue(!$condition, $messageKey, $messageArguments);
		return $error;
	}
}